//
//  ByViewStoreConsume.m
//  mijifen_ios
//
//  Created by mc on 2017/10/31.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByViewStoreConsume.h"

// cell
#import "ByCellStoreConsume.h"

@interface ByViewStoreConsume()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSArray *placeArray;
@property (nonatomic, strong) NSArray *buttonArray;

@property (nonatomic, strong) NSMutableDictionary *infoDic;
@end

@implementation ByViewStoreConsume

- (void)setupUI{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = BY_TABLE_COLOR;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self addSubview:self.tableView];
}

- (void)setFrame:(CGRect)frame{
    [super setFrame:frame];
    self.tableView.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
}

#pragma mark - getter
- (NSArray *)placeArray{
    if (!_placeArray) {
        _placeArray = @[@"请输入会员手机号",@"请输入消费金额",@"请输入积分数量"];
    }
    return _placeArray;
}

- (NSArray *)buttonArray{
    if (!_buttonArray) {
        _buttonArray = @[@"",@"",@"抵扣"];
    }
    return _buttonArray;
}

- (NSMutableDictionary *)infoDic{
    if (!_infoDic) {
        _infoDic = [NSMutableDictionary dictionary];
    }
    return _infoDic;
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.placeArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ByCellStoreConsume *cell = [ByCellStoreConsume cellWithTableView:tableView andPlaceStr:self.placeArray[indexPath.section] andDeduct:self.buttonArray[indexPath.section]];
    if (indexPath.section == 1 && [self.infoDic[@"12"] length] != 0) {
        if ([self.infoDic[@"11"] length] != 0) {
            cell.textField.text = [NSString stringWithFormat:@"%@ + %@元 积分抵扣",self.infoDic[@"11"],self.infoDic[@"12"]];
        }else{
            cell.textField.text = [NSString stringWithFormat:@"%@元 积分抵扣",self.infoDic[@"12"]];
        }
    }
    cell.editBlock = ^(NSString *str) {
        [self.infoDic setValue:str forKey:[NSString stringWithFormat:@"%ld",indexPath.section + 10]];
    };
    __weak typeof(self) weakSelf = self;
    cell.deductBlock = ^(NSString *str) {
        NSLog(@"%@",str);
        [self.infoDic setValue:str forKey:[NSString stringWithFormat:@"%ld",indexPath.section + 10]];
        NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:1];
        [weakSelf.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationNone];
    };
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (section == 1) {
        return [UIView new];
    }else{
        UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 35)];
        UILabel *footerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 15, SCREEN_WIDTH - 20, 20)];
        [footerView addSubview:footerLabel];
        footerView.backgroundColor = [UIColor clearColor];
        footerLabel.font = FONT(16);
        // 测试数据
        if (section == 0) {
            footerLabel.text = @"余额：200";
        }else{
            footerLabel.text = @"剩余 5000蜜积分，可抵扣50元";
        }
        return footerView;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 15;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 1) {
        return 0.0001;
    }
    return 35;
}



@end
